package com.ipcamera.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.ipcamer.demo.R;
import com.ipcamera.demo.bean.ErrorBean;
import com.ipcamera.demo.bean.JsonBean;
import com.ipcamera.demo.net.ApiCallBack;
import com.ipcamera.demo.net.HttpConstances;
import com.ipcamera.demo.net.VcmApi;
import com.ipcamera.demo.utils.Log;
import com.ipcamera.demo.utils.MyStringUtils;
import com.ipcamera.demo.utils.StringUtils;
import com.ipcamera.demo.utils.SystemValue;
import com.ipcamera.demo.utils.ToastUtils;

import org.json.JSONObject;

import vstc2.nativecaller.NativeCaller;

public class FlowInfoActivity extends Activity implements BridgeService.FlowinfoInterface {
    public static final String URL_RENEW_4G_BASE = "https://payment-vstc-china.eye4.cn/";
    public static final String URL_RENEW_4G_status = URL_RENEW_4G_BASE + "G4/v1/card/show";
    TextView devinfo,tvflowinfo;
    Button getInfobutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow_info);

        devinfo = (TextView) findViewById(R.id.tv4ginfo);
        getInfobutton = (Button)findViewById(R.id.getInfobutton);
        tvflowinfo = (TextView)findViewById(R.id.tvflowinfo);

        sendCgi(SystemValue.deviceId, SystemValue.devicePass);
        BridgeService.mflowinfoInterface = FlowInfoActivity.this;
        getInfobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFlowinfo();
            }
        });
    }

    public static void sendCgi(String did, String pwd) {
        NativeCaller.TransferMessage(did,
                "trans_cmd_string.cgi?cmd=2138&command=0&loginuse=admin&loginpas="
                        + pwd + "&user=admin&pwd=" + pwd, 1);
    }


    @Override
    public void infoCallBack(String did, String content) {
        String signal = MyStringUtils.spitValue(content, "signal=");
        String iccid = MyStringUtils.spitValue(content, "iccid=").replace("\"", "");
        String operator = MyStringUtils.spitValue(content, "operator=");
        devinfo.setText("ccid :" + iccid + "\r\n"+"信号："+signal + "\r\n"+ "运营商:"+get4GOperator(operator));
    }

    public static String get4GOperator( String operator){
        if (operator==null){
            operator="";
        }
        if(operator.equals("1"))
        {
            return "中国移动";
        }else if(operator.equals("2"))
        {
            return "中国联通";
        }else if(operator.equals("3"))
        {
            return "中国电信";
        }else
        {
            return "0";
        }
    }

    public static String get4gDeviceFlow( String userid, String ccid) {
        try {
            Object[] args = new Object[3];

            args[0] = userid;
            args[1] = ccid;
            String result = String.format("{\"userid\":\"%s\",\"cNo\":\"%s\"}", args);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //设置语言接口
    public void getFlowinfo() {

        VcmApi.get().load(URL_RENEW_4G_status, get4gDeviceFlow("10605921","8986112021404607943"), new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                try
                {
                    JSONObject obj = new JSONObject(bean.getJson());
                    Log.e("vst",bean.getJson());
                    if (obj.has("lPFlow")) {
                        tvflowinfo.setText("卡状态："+ obj.getString("cState")+"\r\n"+"剩余流量："+obj.getInt("lPFlow") );
                    }else if(obj.has("platformId") && obj.getString("platformId").equals("I001"))
                    {
                        tvflowinfo.setText("剩余流量："+obj.getInt("leftFlow"));
                    }

                   // ToastUtils.show(FlowInfoActivity.this,bean.getJson());
                }catch (Exception e)
                {

                }

            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(FlowInfoActivity.this,bean.getErrorMsg());
            }
        });
    }

}
